//index.js
//获取应用实例
var Bmob = require('../../utils/bmob.js');
const app = getApp()
var minA = 1,
    maxA = 99;
var minB = 1,
    maxB = 99;
var minA_ = 1,
    maxA_ = 9;
var minB_ = 1,
    maxB_ = 9;
var A = 0;
var B = 0;
var proText = "";
var op = "+";
var ans = 0;
var myAnsValue = -1;
var proTimeStart, proTimeEnd;
var longTap = false;
Page({
    data: {
        mproText: 'Ready',
        mop: "+",
        isRight: false,
        inputFocus: false,
        infoTxt: '',
        inputEnable: true,
        inputValue: '',
        userInfo: {},
        hasUserInfo: false,
        canIUse: wx.canIUse('button.open-type.getUserInfo'),
        btn1: '1',
        btn2: '2',
        btn3: '3',
        btn4: '4',
        proAuto: false,
        mycoins: 0,
        touch_start: null,
        touch_end: null,
        wrongQuestions: [], // 错题集
        questions: [],      // 存储所有题目
        currentQuestion: 0, // 当前题目的索引
        totalQuestions: 5,  // 设置为总共5道题
        score: 0,           // 用户得分
    },
    //事件处理函数
    bindViewTap: function () {
        // wx.navigateTo({
        //   url: '../logs/logs'
        // })
    },
    onLoad: function () {
        var coins = wx.getStorageSync('wx_mycoins') || 0;
        var savedWrongQuestions = wx.getStorageSync('wrongQuestions') || []; //从本地存储获取错题集
        this.setData({
            mycoins: coins,
            wrongQuestions: savedWrongQuestions
        });
        // this.setData({ mycoins: coins });
        if (app.globalData.userInfo) {
            this.setData({
                userInfo: app.globalData.userInfo,
                hasUserInfo: true
            })
        } else if (this.data.canIUse) {
            // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
            // 所以此处加入 callback 以防止这种情况
            app.userInfoReadyCallback = res => {
                this.setData({
                    userInfo: res.userInfo,
                    hasUserInfo: true
                })
            }
        } else {
            // 在没有 open-type=getUserInfo 版本的兼容处理
            wx.getUserInfo({
                success: res => {
                    app.globalData.userInfo = res.userInfo
                    this.setData({
                        userInfo: res.userInfo,
                        hasUserInfo: true
                    })
                }
            })
        }


        this.generateQuestions(); // 页面加载时生成多道题


    },

    // 生成多道题目
    generateQuestions: function () {
        let questions = [];
        for (let i = 0; i < this.data.totalQuestions; i++) {
            let question = this.createQuestion(); // 生成一道题目
            questions.push(question);
        }
        this.setData({ questions: questions });
        this.showCurrentQuestion(); // 展示第一道题目
    },
    // 生成一道题目
    createQuestion: function () {
        let A, B, op, ans;
        let ops = ['+', '-', '×', '÷'];
        op = ops[this.randomNum(0, 3)]; // 随机选择运算符

        if (op === "+") {
            A = this.randomNum(minA, maxA);
            B = this.randomNum(minB, 100 - A);
            ans = A + B;
        } else if (op === "-") {
            A = this.randomNum(minA, maxA);
            B = this.randomNum(minB, A);
            ans = A - B;
        } else if (op === "×") {
            A = this.randomNum(minA_, maxA_);
            B = this.randomNum(minB_, maxB_);
            ans = A * B;
        } else if (op === "÷") {
            B = this.randomNum(minB_, maxB_);
            A = B * this.randomNum(minA_, maxA_);
            ans = A / B;
        }

        return {
            A: A,
            B: B,
            op: op,
            ans: ans,
            userAns: null // 用户的答案
        };
    },
    // 显示当前题目
    showCurrentQuestion: function () {
        let currentQuestion = this.data.questions[this.data.currentQuestion];
        let proText = `${currentQuestion.A} ${currentQuestion.op} ${currentQuestion.B} = ?`;
        this.setData({
            mproText: proText,
            inputValue: '',
            infoTxt: ''
        });
    },

    onUnload: function () {
        this.UpdateScore();
    },
    onShareAppMessage: function (res) {
        if (res.from === 'button') {
            // 来自页面内转发按钮
            //console.log(res.target)
        }
        return {
            title: '快来,来和我PK下口算。',
            path: '/pages/index/index',
            success: function (res) {
                // 转发成功
            },
            fail: function (res) {
                // 转发失败
            }
        }
    },
    getUserInfo: function (e) {
        //console.log(e)
        app.globalData.userInfo = e.detail.userInfo
        this.setData({
            userInfo: e.detail.userInfo,
            hasUserInfo: true
        })
    }
    ,
    sltPlus: function () {
        op = "+";
        this.setData({ mop: op });
        this.showPro();
    }
    ,
    sltSubd: function () {
        op = "-";
        this.setData({ mop: op });
        this.showPro();
    }
    ,
    sltMult: function () {
        op = "×";
        this.setData({ mop: op });
        this.showPro();
    },
    sltDivi: function () {
        op = "÷";
        this.setData({ mop: op });
        this.showPro();
    },
    randomNum: function (minNum, maxNum) {
        switch (arguments.length) {
            case 1:
                return parseInt(Math.random() * minNum + 1);
            case 2:
                return parseInt(Math.random() * (maxNum - minNum + 1) + minNum);
            default:
                return 0;
        }
    },
    showPro: function () {
        if (op === "+") {
            A = this.randomNum(minA, maxA);
            maxB = 100 - A;
            B = this.randomNum(minB, maxB);
        } else if (op === "-") {
            A = this.randomNum(minA, maxA);
            maxB = 100 - A;
            B = this.randomNum(minB, maxB);

            A = A + B;
        }
        else if (op === "×") {
            A = this.randomNum(minA_, maxA_);
            B = this.randomNum(minB_, maxB_);
        }
        else if (op === "÷") {
            A = this.randomNum(minA_, maxA_);
            B = this.randomNum(minB_, maxB_);

            A = A * B;
        }
        proText = A + op + B + "=?";
        this.setData({ mproText: proText });
        this.setData({ inputEnable: false });
        myAnsValue = -1;
        this.setData({ inputValue: '' });
        this.setData({ infoTxt: '' });
        this.MakeNumBtn();
        proTimeStart = new Date();

    },
    showAns: function () {
        if (op === "+") {
            ans = A + B;
        } else if (op === "-") {
            ans = A - B;
        } else if (op === "×") {
            ans = A * B;
        } else if (op === "÷") {
            ans = A / B;
        }
        proText = A + op + B + "=" + ans;

        this.setData({ mproText: proText });
    },
    // 当用户在输入框中输入答案时，更新变量 myAnsValue 保存用户的输入值。
    bindKeyInput: function (e) {
        myAnsValue = e.detail.value;
    },
    // 提交答案，调用 checkInput 函数进行答案检查。
    ansSubmit: function () {
        this.checkInput();
    },
    // 当输入框聚焦时，清空输入框和提示信息。
    focusInputEvent: function (e) {
        myAnsValue = -1;
        this.setData({ inputValue: '' });
        this.setData({ infoTxt: '' });
    },
    // 计算正确答案并与用户输入的值进行对比。
    // 如果答案正确，页面显示正确标记，并根据回答时间奖励金币。
    // 如果答案错误，页面显示错误标记。
    checkInput: function () {
        if (op === "+") {
            ans = A + B;
        } else if (op === "-") {
            ans = A - B;
        } else if (op === "×") {
            ans = A * B;
        } else if (op === "÷") {
            ans = A / B;
        }
        //console.log(myAnsValue);

        proTimeEnd = new Date();
        var tsp = proTimeEnd.getTime() - proTimeStart.getTime();
        //console.log(tsp);
        if (myAnsValue.length === undefined) {
            this.setData({ infoTxt: '' });
        } else if (parseInt(myAnsValue) === parseInt(ans)) {
            this.setData({ isRight: true });
            this.setData({ infoTxt: '✔' });
            var coins = parseInt(this.data.mycoins);
            var proCoins = 0;
            if (tsp < 1500) {
                proCoins = 10;
            } else if (tsp < 2500) {
                proCoins = 9;
            } else if (tsp < 3500) {
                proCoins = 8;
            } else if (tsp < 4500) {
                proCoins = 7;
            } else if (tsp < 5500) {
                proCoins = 6;
            } else if (tsp < 6500) {
                proCoins = 5;
            } else if (tsp < 7500) {
                proCoins = 4;
            } else if (tsp < 8500) {
                proCoins = 3;
            } else if (tsp < 9500) {
                proCoins = 2;
            } else {
                proCoins = 1;
            }
            coins = coins + proCoins;
            this.setData({ mycoins: coins });
            wx.showToast({
                title: '喜获 ' + proCoins + ' 个金币', //标题最长7个字
                icon: 'success',
                duration: 1000
            })
            wx.setStorageSync('wx_mycoins', coins);
            //this.UpdateScore();
            if (this.data.proAuto === true) {
                setTimeout(function () {
                    this.showPro();
                }.bind(this), 800);
            }
        }
        else {
            // 答错了，将题目保存到错题集
            const currentQuestion = {
                A: A,
                B: B,
                op: op,
                correctAns: ans,
                userAns: myAnsValue
            };
            const updatedWrongQuestions = this.data.wrongQuestions.concat(currentQuestion);
            // 保存错题集到本地存储
            // wx.setStorageSync('wrongQuestions', this.data.wrongQuestions);

            this.setData({
                isRight: false,
                infoTxt: '✘',
                wrongQuestions: updatedWrongQuestions
            });

            wx.setStorageSync('wrongQuestions', updatedWrongQuestions);

            if (this.data.proAuto === true) {
                setTimeout(function () {
                    this.showPro();
                }.bind(this), 800);
            }
        }

        // let currentQuestion = this.data.questions[this.data.currentQuestion];
        // let userAns = parseInt(this.data.inputValue);
        // if (userAns === currentQuestion.ans) {
        //     this.setData({ infoTxt: '✔' });
        //     this.setData({ score: this.data.score + 10 }); // 每答对一题加10分
        // } else {
        //     this.setData({ infoTxt: '✘' });
        // }

        // // 保存用户答案
        // currentQuestion.userAns = userAns;

        // // 判断是否完成所有题目
        // if (this.data.currentQuestion < this.data.totalQuestions - 1) {
        //     setTimeout(() => {
        //         this.setData({ currentQuestion: this.data.currentQuestion + 1 });
        //         this.showCurrentQuestion(); // 显示下一题
        //     }, 1000);
        // } else {
        //     this.setData({ isQuizFinished: true });
        //     this.showQuizResult(); // 显示结果
        // }
    },

    // 根据用户点击的数字按钮，更新输入框中的值。
    AnsInput: function (e) {
        myAnsValue = this.data.inputValue;
        myAnsValue += e.target.id;
        this.setData({ inputValue: myAnsValue });
    },
    // 将输入框中的值清空，并清除提示信息。
    AnsClear: function () {
        myAnsValue = -1;
        this.setData({ inputValue: '' });
        this.setData({ infoTxt: '' });
    },
    // 计算正确答案后，生成一个包含答案数字和随机数字的数组，显示在页面上的按钮上，供用户选择。
    MakeNumBtn: function () {
        function getRandomArrayElements(arr, count) {
            var shuffled = arr.slice(0), i = arr.length, min = i - count, temp, index;
            while (i-- > min) {
                index = Math.floor((i + 1) * Math.random());
                temp = shuffled[index];
                shuffled[index] = shuffled[i];
                shuffled[i] = temp;
            }
            return shuffled.slice(min);
        }

        if (op === "+") {
            ans = A + B;
        } else if (op === "-") {
            ans = A - B;
        } else if (op === "×") {
            ans = A * B;
        } else if (op === "÷") {
            ans = A / B;
        }
        var nums = '0123456789'
        var arrAns = [];

        var temp = ans.toString().split('');
        for (var i = 0; i < temp.length; i++) {
            if (arrAns.indexOf(temp[i]) == -1) {
                arrAns.push(temp[i]);
            }
            nums = nums.replace(temp[i], '');
        }
        arrAns = getRandomArrayElements(arrAns.concat(getRandomArrayElements(nums.split(''), 4 - arrAns.length)), 4);
        this.setData({ btn1: arrAns[0] });
        this.setData({ btn2: arrAns[1] });
        this.setData({ btn3: arrAns[2] });
        this.setData({ btn4: arrAns[3] });
    },
    // 处理页面上自动出题的开关状态，若开启则在用户答题后自动展示新题目
    switchChange: function (e) {
        this.setData({ proAuto: e.detail.value });
        this.showPro();

    },
    // 使用 wx.navigateTo 导航到分数页面，展示用户的分数。
    showScores: function (event) {
        //this.UpdateScore();
        wx.navigateTo({
            url: '../score/score'
        });
    },
    // clearIcons: function (event) {
    //     let that = this;
    //     //触摸时间距离页面打开的毫秒数  
    //     // var touchTime = that.data.touch_end - that.data.touch_start;
    //     //console.log(touchTime);
    //     //如果按下时间大于350为长按  
    //     // if (touchTime > 350) {
    //     wx.showModal({
    //         title: '提示',
    //         content: '是否清零金币？',
    //         success: function (res) {
    //             if (res.confirm) {
    //                 that.setData({ mycoins: 0 });
    //                 wx.setStorageSync('wx_mycoins', 0);
    //                 //this.UpdateScore();
    //                 //console.log(res)
    //                 wx.showToast({
    //                     title: '清除成功！',
    //                     icon: 'success',
    //                     duration: 2000
    //                 })
    //             }
    //         }
    //     })
    //     //} else {
    //     //

    //     // }
    // },
    //按下事件开始  
    mytouchstart: function (e) {
        let that = this;
        that.setData({
            touch_start: e.timeStamp
        })
        console.log(e.timeStamp + '- touch-start')
    },
    //按下事件结束  
    mytouchend: function (e) {
        let that = this;
        that.setData({
            touch_end: e.timeStamp
        })
        console.log(e.timeStamp + '- touch-end')
    },
    UpdateScore: function () {
        var Score = Bmob.Object.extend("UserScore");
        var query = new Bmob.Query(Score);
        //console.log(app.globalData.opendId.toString());
        query.equalTo("wxOpenId", app.globalData.opendId.toString());
        query.descending("UserScore");
        query.find({
            success: function (results) {
                if (results.length > 0) {
                    var score = results[0];
                    score.set("UserId", app.globalData.userInfo.nickName.toString());
                    score.set('UserScore', wx.getStorageSync('wx_mycoins') || 0);
                    score.set('UserAvatarUrl', app.globalData.userInfo.avatarUrl.toString());
                    score.save();
                }
                else {
                    var score = new Score();
                    score.set("wxOpenId", app.globalData.opendId.toString());
                    score.set("UserId", app.globalData.userInfo.nickName.toString());
                    score.set("UserScore", wx.getStorageSync('wx_mycoins') || 0);
                    score.set('UserAvatarUrl', app.globalData.userInfo.avatarUrl.toString());
                    //添加数据，第一个入口参数是null
                    score.save(null, {
                        success: function (result) {
                            //console.log("成功, objectId:" + result.id);
                        },
                        error: function (result, error) {

                        }
                    });
                }

            },
            error: function (result, error) {
                //console.log('更新失败' + error);

            }
        });
    },
    showWrongQuestions: function () {
        wx.navigateTo({
            url: '/pages/index/wrongQuestion'
        });
    }
})
